//
//  QBAlbumsViewController.m
//  QBImagePicker
//
//  Created by Katsuma Tanaka on 2015/04/06.
//  Copyright (c) 2015 Katsuma Tanaka. All rights reserved.
//

#import "SimAlbumsListVC.h"
#import "SimAlbumCell.h"
#import "SimMulImagePickerVC.h"
#import "SimAssetsCollectionVC.h"
#import "UIBarButtonItem+SimAdditions.h"

@interface SimAlbumsListVC ()
{
}

@property (nonatomic, strong) ALAssetsLibrary *assetsLibrary;
@property (nonatomic, strong, readonly) NSArray *assetsGroups;

@end

@implementation SimAlbumsListVC

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.title = @"照片";
    self.navigationItem.prompt = self.imagePicker.prompt;
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithText:@"取消" target:self action:@selector(doCancel)];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setToolbarHidden:YES animated:YES];
}

- (NSArray *)assetsGroups
{
    return self.imagePicker.assetsGroups;
}

- (void)doCancel
{
    if ([self.imagePicker.delegate respondsToSelector:@selector(simImagePickerDidCancel:)]) {
        [self.imagePicker.delegate simImagePickerDidCancel:self.imagePicker];
    }
}

#pragma mark - Checking for Selection Limit

- (BOOL)isMinimumSelectionLimitFulfilled
{
    return (self.imagePicker.minimumNumberOfSelection <= self.imagePicker.selectedAssets.count);
}

- (BOOL)isMaximumSelectionLimitReached
{
    NSUInteger minimumNumberOfSelection = MAX(1, self.imagePicker.minimumNumberOfSelection);
    
    if (minimumNumberOfSelection <= self.imagePicker.maximumNumberOfSelection) {
        return (self.imagePicker.maximumNumberOfSelection <= self.imagePicker.selectedAssets.count);
    }
    
    return NO;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    SimAssetsCollectionVC *assetsVC = [[SimAssetsCollectionVC alloc] init];
    assetsVC.imagePicker = self.imagePicker;
    assetsVC.assetsGroup = self.imagePicker.assetsGroups[self.tableView.indexPathForSelectedRow.row];
    [self.navigationController pushViewController:assetsVC animated:YES];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    return 55;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.assetsGroups.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identify = @"AlbumCell";
    SimAlbumCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
    if (!cell) {
        cell = [[SimAlbumCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identify];
    }
    
    cell.tag = indexPath.row;
    cell.assetsGroup = self.assetsGroups[indexPath.row];
        
    return cell;
}



@end
